package com.situ.mall.common.resolver;

import com.situ.mall.common.exception.MyException;
import com.situ.mall.common.exception.NoLoginException;
import com.situ.mall.common.vo.ResultVO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author yangkx
 * @create 2023-09-13-0:09
 */
//异常接收器
@ControllerAdvice
public class MyExceptionResolver {

    //未登录异常
    @ExceptionHandler(NoLoginException.class)
    public ResponseEntity t3(NoLoginException e){

        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
    }

    //拦截自定义的异常
    @ExceptionHandler(MyException.class)
    public ResponseEntity t1(MyException e){
        //把异常中的信息取出来，放到ResultVO里面
        ResultVO vo = new ResultVO();
        vo.setCode(-1);
        vo.setMsg(e.getMsg());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(vo);
    }

    //拦截自定义以外的所有异常
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity t2(Exception e){
        //在控制台打印异常信息
        e.printStackTrace();
        ResultVO vo = new ResultVO();
        vo.setCode(-1);
        vo.setMsg("服务器内部处理错误，请稍后再试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(vo);
    }


}
